//
//  DQAdViewController.m
//  DQBaisibudejie
//
//  Created by 杜倩 on 16/8/31.
//  Copyright © 2016年 杜倩. All rights reserved.
/*
 进入页面的广告
 
 //版本新特性  广告界面
 1 window的跟控制器 广告界面
 然后  定时器  时间到0秒后  再把window的跟控制器设置为TabBar
 cocoapods ---- 用来管理第三方框架,不用去配置文件
 
 */

#import "DQAdViewController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "DQADModel.h"
#import "UIImageView+WebCache.h"
#import "DQtabBarController.h"
@interface DQAdViewController ()

- (IBAction)jump:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnAttr;
@property (weak, nonatomic) IBOutlet UIView *AdView;//AFN

@property(nonatomic,strong)DQADModel *model;

@property(nonatomic,strong)UIImageView *imgView;
@property(nonatomic,strong)NSTimer *timer;

@property(nonatomic,assign)CGFloat h;
@property(nonatomic,assign)CGFloat w;

@end

@implementation DQAdViewController

#pragma mark 懒加载
-(UIImageView *)imgView{
    if (_imgView==nil) {
        //w为nsnumber 转换为 cgfloat
        CGFloat screenW=  self.view.bounds.size.width;
        if (_h) {
            _imgView=[[UIImageView alloc]
                      initWithFrame:CGRectMake(0, 0,screenW, _h/_w*screenW)];//self.view.bounds设置view的大小
        }else{
            _imgView=[[UIImageView alloc] initWithFrame:self.view.bounds];
        }
        //懒加载继续就使用该
        _imgView.userInteractionEnabled=YES;//允许交互
        //添加手势
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [_imgView addGestureRecognizer:tap];
    }
    return _imgView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    //设置定时器--当不用定时器的时候一定要销毁-否则跳转页面后还会在递减
    _timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(second) userInfo:nil repeats:YES];

}
#pragma mark 定时器的单击事件
-(void)second{
  static  int second1=5;//
    //拼接广告页面时间按钮的 -- 5s|跳转
    NSString *str=[NSString stringWithFormat:@"%ds|跳转",second1];
    [_btnAttr setTitle:str forState:UIControlStateNormal];
    if (second1==0) {
        //tableView还是根控制器
        //获取主窗口--keyWindow  并且改变根控制器
        DQtabBarController *tabbar=[[DQtabBarController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController=tabbar;
        [_timer invalidate];//销毁定时器
    }
    second1 --;//秒数减
    
}
#pragma mark 手势的单击事件
-(void)click:(UITapGestureRecognizer *)sender{
    NSURL *url=[NSURL URLWithString:_model.rl];
    //打开应用级别的程序  需要用UIApplication
    [[UIApplication sharedApplication] openURL:url];
    
}


#pragma mark 加载数据
-(void)loadData{
    //AFN使用步骤--1、建立管理者  GET POST JSON XML格式
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    //请求数据--接口文档提示格式
    NSString *url=@"http://mi.gdt.qq.com/gdt_mview.fcg?posw=640&posid=1090214194677244&ext=%7B%22req%22%3A%7B%22c_osver%22%3A%229.3.5%22%2C%22sdk_src%22%3A%22%22%2C%22c_isjailbroken%22%3Afalse%2C%22muidtype%22%3A2%2C%22jsver%22%3A%224.3.000%22%2C%22c_device%22%3A%22iPhone%205s%22%2C%22c_h%22%3A1136%2C%22muid%22%3A%22080ed9f84b8a0be8974b81000124b003%22%2C%22lng%22%3A0%2C%22c_pkgname%22%3A%22com.spriteapp.baisibdj%22%2C%22c_os%22%3A%22ios%22%2C%22scs%22%3A%22000126d70bb2%22%2C%22conn%22%3A1%2C%22c_devicetype%22%3A1%2C%22c_w%22%3A640%2C%22carrier%22%3A1%2C%22c_sdfree%22%3A3038257152%2C%22lat%22%3A0%2C%22c_ori%22%3A0%2C%22c_dpi%22%3A320%2C%22sdkver%22%3A%224.3%22%2C%22deep_link_version%22%3A1%2C%22postype%22%3A4%2C%22tmpallpt%22%3Atrue%2C%22loc_accuracy%22%3A0%7D%7D&count=1&posh=1136&datatype=2&adposcount=1";
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"%@",responseObject);
        //请求数据-->解析数据-->plist文件（直接在桌面添加）--->字典转模型--->显示数据
        //将数据写到plist文件里
    [responseObject writeToFile:@"/Users/DuQian/Desktop/qw.plist" atomically:YES];
        
        //解析数据
//        NSLog(@"%@",model);
        NSDictionary *dict=responseObject[@"data"];//data-plist 文件的key值
        NSDictionary *dict1=dict[@"1090214194677244"];
        NSDictionary *dict2=dict1[@"cfg"];
        NSDictionary *dict3=dict2[@"playcfg"];
        //----------求宽高的模型--宽高比
        _model=[DQADModel adModelWithDict:dict3];
        
        if (_model.h) {
             _w=[_model.w doubleValue];
             _h=[_model.h doubleValue];

        }
        
        
 //------------求图片地址的模型
        NSArray *array=dict1[@"list"];
               // NSLog(@"%@",array);
        //
        _model=[DQADModel adModelWithDict:array[0]];
       // NSLog(@"%@",_model.img);//拿到model的值
        //请求数据
        if (_model.img) {//若请求下数据就运行以下代码
            
            //赋值
            NSURL *url=[NSURL URLWithString:_model.img];
            [self.imgView sd_setImageWithURL:url];//---第三方
           //NSLog(@"%@",_imgView.image);
            [self.AdView addSubview:_imgView];
            //设置透明度
            self.AdView.alpha =1;
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    
}
#pragma mark 按钮的方法--单击跳转到页面
- (IBAction)jump:(id)sender {
    //tabBar还是根控制器
    DQtabBarController *tabbar=[[DQtabBarController alloc] init];
    //获取主窗口--keyWindow  并且改变根控制器
    [UIApplication sharedApplication].keyWindow.rootViewController=tabbar;
    //销毁计时器
    [_timer invalidate];
    
    
}
@end
